/**
 * 秒数转带格式的时间字符串
 * @param {*} value 
 */
const sec2timeStr = value => {
    let theTime = parseInt(value);
    let theTime1 = 0;
    let theTime2 = 0;
    if (theTime >= 60) {
        theTime1 = parseInt(theTime / 60);
        theTime = parseInt(theTime % 60);
        if (theTime1 >= 60) {
            theTime2 = parseInt(theTime1 / 60);
            theTime1 = parseInt(theTime1 % 60);
        }
    }
    if (theTime < 10) {
        theTime = "0" + parseInt(theTime)
    }
    let result = "" + theTime + "";
    if (theTime1 >= 0) {
        if (theTime1 < 10) {
            theTime1 = "0" + parseInt(theTime1)
        }
        result = "" + theTime1 + ":" + result;
    }
    if (theTime2 >= 0) {
        if (theTime2 < 10) {
            theTime2 = "0" + parseInt(theTime2)
        }
        result = "" + theTime2 + ":" + result;
    }
    return result;
};

/**
 * 带格式的时间字符串转秒数
 * @param {*} timeStr 
 */
const timeStr2sec = timeStr => {
    let t_arr = timeStr.split(":");
    let hh = t_arr[0];
    let mm = t_arr[1];
    let ss = t_arr[2];
    let s_now = parseInt(hh * 3600) + parseInt(mm * 60) + parseInt(ss);
    return s_now;
};

/**
 * 时间对象转带格式的时间字符串
 * @param {*} obj "new Date()"
 * @param {*} format "yyyy-MM-dd hh:mm:ss"
 */
const dateObject2timeStr = (obj, format) => {
    var o = {
        "M+": obj.getMonth() + 1, //month
        "d+": obj.getDate(), //day
        "h+": obj.getHours(), //hour
        "m+": obj.getMinutes(), //minute
        "s+": obj.getSeconds(), //second
        "q+": Math.floor((obj.getMonth() + 3) / 3), //quarter
        "S": obj.getMilliseconds() //millisecond
    }

    if (/(y+)/.test(format)) {
        format = format.replace(RegExp.$1, (obj.getFullYear() + "").substr(4 - RegExp.$1.length));
    }

    for (let k in o) {
        if (new RegExp("(" + k + ")").test(format)) {
            format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return format;
};

export { sec2timeStr, timeStr2sec, dateObject2timeStr };